const db = require('../../dataBase/index')
const moment = require('moment')

const usersHandle = {
  // 获取用户信息列表
  getUserInfo(req, res) {
    const pagenum = req.query.pagenum // 当前页码1
    const pagesize = req.query.pagesize  // 显示条数2
    const query = req.query.query
    sqlStr = `SELECT * FROM users WHERE username like '%${query}%' LIMIT ${pagesize} OFFSET ${pagesize * (pagenum - 1)}`
    db.query(sqlStr, (err, results) => {
      if (err) return res.cwcl('数据库查询语句失败！' + err)
      sqlSelectStr = 'SELECT * FROM users'
      db.query(sqlSelectStr, (err, result) => {
        if (err) return res.cwcl('数据库查询语句失败！' + err)
        res.send({
          meta: {
            code: 200,
            msg: '请求数据成功！'
          },
          data: {
            totalPage: result.length,  // 总条数
            pagenum: req.body.pagenum,  // 当前页码
            users: results
          }
        })
      })
    })
  },
  // 更改用户信息中的状态
  reviseUserInfoState(req, res) {
    const putData = req.body
    sqlReviseStr = `UPDATE users SET mg_state=${putData.state} where id=${putData.uId}`
    db.query(sqlReviseStr, (err, results) => {
      if (err) return res.cwcl('数据库更新语句失败！' + err)

      res.send({
        meta: {
          code: 200,
          msg: '更新用户状态成功！'
        }
      })
    })
  },
  // 添加用户
  addUser(req, res) {
    const userData = req.body
    if (userData.role_name == '') {
      userData.role_name = '测试'
    }
    console.log(userData.create_time);
    let creatTimeLen = userData.create_time.length
    if (creatTimeLen == 0) {
      userData.create_time = `${moment().format('L')}/${moment().format('LT')}`
    }
    const sqlInsertStr = `INSERT INTO users VALUES 
        (NULL,'${userData.username}','${userData.password}',${userData.mobile},'${userData.eMail}','${userData.role_name}','${userData.create_time}',0,1,0)`
    const sqlSelectStr = `SELECT * FROM users WHERE username='${userData.username}'`
    db.query(sqlSelectStr, (err, results) => {
      if (err) return res.cwcl('数据库查询语句执行失败！' + err)
      if (results.length > 0) return res.cwcl('用户已存在！')
      this.selectRes = results
      db.query(sqlInsertStr, (err, results) => {
        if (err) return res.cwcl('数据库插入语句失败！' + err)
        db.query(sqlSelectStr, (err, results) => {
          if (err) return res.cwcl('数据库查询语句执行失败！' + err)
          if (results[0].password !== '') {
            results[0].password = ''
          }
          res.send({
            meta: {
              code: 200,
              msg: '添加用户成功！'
            },
            data: results
          })
        })
      })
    })
  },
  // 根据 id 查询用户信息
  selectUserForId(req, res) {
    const id = req.query.id
    sqlSelectStr = `SELECT * FROM users WHERE id = ${id} `
    db.query(sqlSelectStr, (err, results) => {
      if (err) return cwcl('数据库查询语句执行失败！' + err)
      results[0].password = '不能给你'
      res.send({
        meta: {
          code: 200,
          mag: '用户查询成功！'
        },
        data: results
      })
    })
  },
  // 修改用户信息
  editUserInfo(req, res) {
    const queryInfo = req.body
    sqlUpdateStr = `UPDATE users SET mobile=${queryInfo.mobile},eMail='${queryInfo.eMail}' where id=${queryInfo.id}`
    db.query(sqlUpdateStr, (err, results) => {
      if (err) return res.cwcl('数据库修改语句执行失败！' + err)

      res.send({
        meta: {
          code: 200,
          msg: '用户信息修改成功！'
        },
        data: queryInfo
      })
    })
  },
  // 根据 id 删除用户
  deleteUser(req, res) {
    const id = req.params.id
    sqlDeleteStr = `DELETE FROM users WHERE id = ${id}`
    db.query(sqlDeleteStr, (err, results) => {
      if (err) return res.cwcl('数据库删除语句执行失败！' + err)

      res.send({
        meta: {
          code: 200,
          msg: '用户删除成功！'
        }
      })
    })
  },
  // 根据 id 分配角色权限
  setUserRole(req, res) {
    const queryInfo = req.body
    `UPDATE users SET role_name = '管理员' where id=1`
    sqlUpdateStr = `UPDATE users SET role_name = '${queryInfo.setuserInfo}' where id=${queryInfo.id}`
    db.query(sqlUpdateStr, (err, results) => {
      if (err) return res.cwcl('数据库修改语句执行失败！')

      res.send({
        meta: {
          code: 200,
          msg: '用户角色分配成功！'
        }
      })
    })
  }
}



module.exports = usersHandle